package com.dog.basics.service.common.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Validator;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dog.basics.bean.model.common.DogComPositioninfo;
import com.dog.basics.bean.vo.api.common.AllPositioninfoVo;
import com.dog.basics.bean.vo.api.common.PositioninfoVo;
import com.dog.basics.dao.common.DogComPositioninfoMapper;
import com.dog.basics.service.common.IDogComPositioninfoService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 省市区信息 服务实现类
 * </p>
 *
 * @author zmr
 * @since 2020-05-27
 */
@Service
@AllArgsConstructor
public class DogComPositioninfoServiceImpl extends ServiceImpl<DogComPositioninfoMapper, DogComPositioninfo> implements IDogComPositioninfoService {


    /**
     * 递归查询全称
     *
     * @param
     * @return
     */
    private String getfullName(DogComPositioninfo obj) {
        if (!Validator.equal(obj.getParentId(), 0)) {
            DogComPositioninfo dogComPositioninfo = getById(obj.getParentId());
            return getfullName(dogComPositioninfo) + obj.getAreaName() + ',';
        } else {
            return "中国,";
        }
    }


    /**
     * @Description: 全国省市区信息
     * @Param:
     * @return:
     * @Author: zhangmr
     * @Date:
     */
    @Override
    public AllPositioninfoVo getAllPositioninfo() {
        AllPositioninfoVo allPositioninfoVo = new AllPositioninfoVo();
        allPositioninfoVo.setAreaId(0L);
        getAllPositioninfo(allPositioninfoVo);
        return allPositioninfoVo;
    }


    private void getAllPositioninfo(AllPositioninfoVo allPositioninfoVo) {
        //查询子节点信息
        Long areaId = allPositioninfoVo.getAreaId();
        List<DogComPositioninfo> dogComPositioninfoList = getBaseMapper().selectList(Wrappers.<DogComPositioninfo>lambdaQuery().eq(DogComPositioninfo::getParentId, areaId).eq(DogComPositioninfo::getIsUse, 0));
        if (CollUtil.isNotEmpty(dogComPositioninfoList)) {
            List<AllPositioninfoVo> allPositioninfoVoList = dogComPositioninfoList.stream().map(e -> {
                AllPositioninfoVo vo = new AllPositioninfoVo();
                BeanUtil.copyProperties(e, vo);
                return vo;
            }).collect(Collectors.toList());
            allPositioninfoVo.setAllPositioninfoVoList(allPositioninfoVoList);
            for (AllPositioninfoVo cAllPositioninfoVo : allPositioninfoVoList) {
                //递归
                getAllPositioninfo(cAllPositioninfoVo);
            }
        }
    }

    /**
     * @Description: 通过父ID获取子省市区信息
     * @Param:
     * @return:
     * @Author: zhangmr
     * @Date:
     */
    @Override
    public List<com.dog.basics.bean.vo.api.common.PositioninfoVo> getPositioninfo(Long id) {
        //查询子节点信息
        List<DogComPositioninfo> dogComPositioninfoList = getBaseMapper().selectList(Wrappers.<DogComPositioninfo>lambdaQuery().eq(DogComPositioninfo::getParentId, id).eq(DogComPositioninfo::getIsUse, 0));
        Validator.validateTrue(CollUtil.isNotEmpty(dogComPositioninfoList), "common.noData");
        List<PositioninfoVo> positioninfoVoList = dogComPositioninfoList.stream().map(e -> BeanUtil.toBean(e, PositioninfoVo.class)).collect(Collectors.toList());
        return positioninfoVoList;
    }

}
